package com.un.ebs.core.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.un.ebs.core.config.OSSConfig;
import com.un.ebs.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class SmsUtils {

    private static OSSConfig config;
    private static final Logger logger = LoggerFactory.getLogger(SmsUtils.class);

    @Autowired
    private OSSConfig ossConfig;

    public static void sendRegisterSms(String phone, String code, String signName, String templateCode) {
        if(StringUtils.isNullOrEmpty(phone)){
            throw new BusinessException("手机号码不能为空。");
        }
        if (StringUtils.isNullOrEmpty(code)) {
            throw new BusinessException("验证码不能为空。");
        }
        if (StringUtils.isNullOrEmpty(signName)) {
            throw new BusinessException("验证码签名不能为空。");
        }
        if (StringUtils.isNullOrEmpty(templateCode)) {
            throw new BusinessException("短信模板不能为空。");
        }
        if (StringUtils.isNullOrEmpty(config.getAccessKeyId())) {
            throw new BusinessException("accessKey id : is null.");
        }
        if (StringUtils.isNullOrEmpty(config.getAccessKeySecret())) {
            throw new BusinessException("access key secret : is null");
        }
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", config.getAccessKeyId(), config.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);

        JSONObject param = new JSONObject();
        param.put("code", code);
        param.toString();
        request.putQueryParameter("TemplateParam", param.toString());

        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject res = JSON.parseObject(response.getData());
            if (!"OK".equals(res.getString("Code"))) {
                throw new BusinessException(res.getString("Message"));
            }
        } catch (ServerException e) {
            logger.error("发送验证码失败(" + phone + " : " + code + " ：" + e.getMessage());
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        } catch (ClientException e) {
            logger.error("发送验证码失败(" + phone + " : " + code + " ：" + e.getMessage());
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }

    public static void sendRegisterSms(String phone, String code) {
        sendRegisterSms(phone, code, "微工单", "SMS_178995741");
    }

    @PostConstruct
    private void init() {
        config = ossConfig;
    }
}
